import { BadRequestException, Module } from '@nestjs/common';
import { UserController } from './api/user.controller';
import { StorageController } from './api/storage.controller';
import { SettingController } from './api/setting.controller';
import { LoginController } from './api/login.controller';
import { AccountController } from './api/account.controller';
import { TokenService } from './service/token.service';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer'
import fs from 'fs'
import dayjs from 'dayjs';
import { randomUUID } from 'crypto';
import { ChatController } from './api/chat.controller';
import { PayService } from './service/pay.service';
import { WxpayCallbackController } from './api/wxpay.callback.controller';
import { OrderController } from './api/order.controller';
import { WeixinService } from './service/weixin.service';
import { WxController } from './api/wx.controller';
import { BullModule } from '@nestjs/bull';
import { QiniuService } from './service/qiniu.service';
import { PagesController } from './api/pages.controller';
import { MediaController } from './api/media.controller';
import { OrgnizeController } from './api/orgnize.ccontroller';

const image = ['gif', 'png', 'jpg', 'jpeg', 'bmp', 'webp'];
const video = ['mp4', 'webm'];
const audio = ['mp3', 'wav', 'ogg'];

const checkDirAndCreate = filePath => {
  const pathArr = filePath.split('/');
  let checkPath = '.';
  let item: string;
  for (item of pathArr) {
    checkPath += `/${item}`;
    if (!fs.existsSync(checkPath)) {
      fs.mkdirSync(checkPath);
    }
  }
}

@Module({
  controllers: [
    AccountController,
    UserController,
    SettingController,
    LoginController,
    StorageController,
    ChatController,
    WxpayCallbackController,
    OrderController,
    WxController,
    PagesController,
    MediaController,
    OrgnizeController
  ],
  providers: [TokenService, PayService,WeixinService,QiniuService],
  imports: [
    MulterModule.register({
      storage:diskStorage({
        destination : function(req,file,cb){
          
          // 根据上传的文件类型将图片视频音频和其他类型文件分别存到对应英文文件夹
          const mimeType = file.mimetype.split('/')[1];
          let temp = 'other';
          image.filter(item => item === mimeType).length > 0
            ? (temp = 'image')
            : '';
          video.filter(item => item === mimeType).length > 0
            ? (temp = 'video')
            : '';
          audio.filter(item => item === mimeType).length > 0
            ? (temp = 'audio')
            : '';
          const filePath = `public/uploads/${temp}/${dayjs().format(
            'YYYY-MM-DD',
          )}`;
          checkDirAndCreate(filePath); // 判断文件夹是否存在，不存在则自动生成
          return cb(null, `./${filePath}`);
        },
        filename: (req, file, cb) => {
          // 在此处自定义保存后的文件名称
          const fileType = file.originalname.split('.');
          const filename = `${randomUUID()}.${fileType[fileType.length - 1]}`;
          return cb(null, filename);
        }
      })
      ,
      fileFilter(req, file, cb) {
        const mimeType = file.mimetype.split('/')[1].toLowerCase();
        let temp = 'other';
        image.filter(item => item === mimeType).length > 0
          ? (temp = 'image')
          : '';
        video.filter(item => item === mimeType).length > 0
          ? (temp = 'video')
          : '';
        audio.filter(item => item === mimeType).length > 0
          ? (temp = 'audio')
          : '';
        if (temp === 'other') {
          return cb(new BadRequestException('文件格式错误！'), false);
        }
        return cb(null, true);
      }
    }),
    BullModule.forRoot('office_queue_config',{
      redis: {
        host: '192.168.1.6',
        password:'b88888888',
        port: 6379,
        db:3
      },
      prefix:'office-mqueue',
    }),
    BullModule.registerQueue({
      configKey: 'office_queue_config',
      name: 'office_queue',
      
    }),
  ],
})

export class CommonModule {}
